﻿using ExpressesClass.Model;
using ExpressesClass.SfApi.Base;
using ExpressesClass.SfApi.billApplyNotify.request;
using ExpressesClass.SfApi.billApplyNotify.response;
using ExpressesClass.SfApi.billCancelNotify.request;
using ExpressesClass.SfApi.billCancelNotify.response;
using ExpressesClass.SfApi.billTraceNotify.request;
using ExpressesClass.SfApi.billTraceNotify.response;
using ExpressesClass.SfApi.billZdNotify.request;
using ExpressesClass.SfApi.billZdNotify.response;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ExpressesClass.IExpresses
{
    public class SfExpress : IExpress
    {
        //顺丰快递参数
        private static string url = "http://bsp-oisp.sf-express.com/bsp-oisp/sfexpressService";
        private static string clientCode = "";
        private static string checkword = "";
        private static string custid = "";
        //顺丰测试参数
        //private static string url = "https://sfapi-sbox.sf-express.com/sfexpressService";
        //private static string clientCode = "";
        //private static string checkword = "";
        //private static string custid = "";

        public Kddh GetMailNo(Kddh kddh)
        {
            Order order = new Order();
            order.orderid = kddh.orderId;
            order.express_type = kddh.shipName == "顺丰特快" ? "1" : "2";
            order.j_contact = kddh.sendMan;
            order.j_tel = kddh.sendManPhone;
            order.j_province = kddh.sendProvince;
            order.j_city = kddh.sendCity;
            order.j_county = kddh.sendCounty;
            order.j_address = kddh.sendManAddress;
            order.j_company = kddh.sendCompany;
            order.d_contact = kddh.receiveMan;
            order.d_tel = kddh.receiveManPhone;
            order.d_province = kddh.receiveProvince;
            order.d_city = kddh.receiveCity;
            order.d_county = kddh.receiveCounty;
            order.d_address = kddh.receiveManAddress;
            order.d_company = kddh.receiveCompany;
            order.parcel_quantity = "1";
            order.pay_method = "1";
            order.custid = kddh.payMethod == "现金支付" ? "" : custid;
            order.cargo = kddh.itemName;
            order.remark = kddh.remark;
            order.pay_method = kddh.payMethod == "收方支付" ? "2" : "1";
            order.need_return_tracking_no = kddh.isReturnNo == "是" ? "1" : "";
            order.routelabelForReturn = kddh.isReturnNo == "是" ? "1" : "";
            order.routelabelService = kddh.isReturnNo == "是" ? "1" : "";

            Request req = new Request("OrderService");
            Response res = req.Execute(url, order, clientCode, checkword);

            if (res != null && res.Result())
            {
                OrderResponse orderResponse = res.GetRes<OrderResponse>();
                rls_detail detail = orderResponse.rls_info.rls_detail;

                string mailno_zd = string.Empty;
                if (int.Parse(kddh.itemCount) > 1)
                {
                    OrderZD orderZD = new OrderZD();
                    orderZD.orderid = kddh.orderId;
                    orderZD.parcel_quantity = (int.Parse(kddh.itemCount) - 1).ToString();
                    Request req_zd = new Request("OrderZDService");
                    Response res_zd = req_zd.Execute(url, orderZD, clientCode, checkword);

                    if (res != null && res.Result())
                    {
                        OrderZDResponse orderZDResponse = res_zd.GetRes<OrderZDResponse>();
                        mailno_zd = orderZDResponse.orderZDResponse.mailno_zd;
                    }
                    else if (res != null && !res.Result())
                    {
                        MessageBox.Show(res.Err(), "提示");
                    }
                    else
                    {
                        MessageBox.Show("获取子单失败.", "提示");
                    }
                }

                Kddh k = new Kddh();
                k.orderId = kddh.orderId;
                k.mailNo = detail.waybillNo;
                k.mailno_zd = mailno_zd;
                k.sendMan = kddh.sendMan;
                k.sendManPhone = kddh.sendManPhone;
                k.sendManAddress = kddh.sendManAddress;
                k.sendPostcode = kddh.sendPostcode;
                k.sendProvince = kddh.sendProvince;
                k.sendCity = kddh.sendCity;
                k.sendCounty = kddh.sendCounty;
                k.sendCompany = kddh.sendCompany;
                k.receiveMan = kddh.receiveMan;
                k.receiveManPhone = kddh.receiveManPhone;
                k.receiveManAddress = kddh.receiveManAddress;
                k.receivePostcode = kddh.receivePostcode;
                k.receiveProvince = kddh.receiveProvince;
                k.receiveCity = kddh.receiveCity;
                k.receiveCounty = kddh.receiveCounty;
                k.receiveCompany = kddh.receiveCompany;
                k.shipName = kddh.shipName;
                k.shipVal = kddh.shipVal;
                k.itemName = kddh.itemName;
                k.itemWeight = kddh.itemWeight;
                k.itemCount = kddh.itemCount;
                k.remark = kddh.remark;
                k.bm = kddh.bm;
                k.wzh = kddh.wzh;
                k.proCode = detail.proCode;
                k.proName = detail.proName;
                k.destRouteLabel = detail.destRouteLabel;
                k.destTeamCode = detail.destTeamCode;
                k.codingMapping = detail.codingMapping;
                k.sourceTransferCode = detail.sourceTransferCode;
                k.abFlag = detail.abFlag;
                k.twoDimensionCode = detail.twoDimensionCode;
                k.codingMappingOut = detail.codingMappingOut;
                k.xbFlag = detail.xbFlag;
                k.printIcon = detail.printIcon;
                k.insuranceValue = "0";
                k.kdCompanys = kddh.kdCompanys;
                k.scenarios = kddh.scenarios;
                k.printNum = kddh.printNum;
                k.payMethod = kddh.payMethod;
                k.isReturnNo = kddh.isReturnNo;
                return k;
            }
            else if (res != null && !res.Result())
            {
                MessageBox.Show(res.Err(), "提示");
            }
            else
            {
                MessageBox.Show("获取快递电子面单失败.", "提示");
            }
            return null;
        }
        public bool CancelMailNo(Kddh kddh)
        {
            OrderConfirm orderConfirm = new OrderConfirm();
            orderConfirm.orderid = kddh.orderId;
            orderConfirm.dealtype = "2";

            Request req = new Request("OrderConfirmService");
            Response res = req.Execute(url, orderConfirm, clientCode, checkword);

            if (res != null && res.Result())
            {
                OrderConfirmResponse orderConfirmResponse = res.GetRes<OrderConfirmResponse>();
                if (orderConfirmResponse.res_status == "2")
                {
                    return true;
                }
                else
                {
                    MessageBox.Show("客户订单号与顺丰运单不匹配", "提示");
                }
            }
            else if (res != null && !res.Result())
            {
                MessageBox.Show("面单号:" + kddh.mailNo + " ," + res.Err(), "提示");
            }
            else
            {
                MessageBox.Show("面单号:" + kddh.mailNo + " ,取消快递电子面单失败,请重试.", "提示");
            }
            return false;
        }
        public List<TraceLog> GetExpressMsg(Kddh kddh)
        {
            RouteRequest routeRequest = new RouteRequest();
            routeRequest.tracking_type = "1";
            routeRequest.method_type = "1";
            routeRequest.tracking_number = kddh.mailNo;

            Request req = new Request("RouteService");
            Response res = req.Execute(url, routeRequest, clientCode, checkword);

            if (res != null && res.Result())
            {
                List<TraceLog> log = new List<TraceLog>();
                RouteResponse routeResponse = res.GetRes<RouteResponse>();
                if (routeResponse == null || routeResponse.Route == null)
                {
                    routeResponse = new RouteResponse();
                    routeResponse.Route = new List<Route>();
                }
                foreach (Route r in routeResponse.Route)
                {
                    TraceLog t = new TraceLog();
                    t.acceptAddress = r.accept_address;
                    t.acceptTime = r.accept_time;
                    t.scanType = r.opcode;
                    t.remark = r.remark;
                    log.Add(t);
                }
                return log;
            }
            else if (res != null && !res.Result())
            {
                MessageBox.Show(res.Err(), "提示");
            }
            else
            {
                MessageBox.Show("查询失败", "提示");
            }
            return null;
        }
        public void GetUseNum()
        {
            MessageBox.Show("顺丰快递是月结形式.", "提示");
        }
    }
}
